define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/stats/models'),
        utils = require('js/utils/tools'),
        app = Backbone.history;
    require('multiselect');
    require('datetimepicker');

    var TransactionListView = Backbone.View.extend({
        tagName: "div",
        events: {
            'click #search': 'doSearch',
            'click #exportData': 'exportData',
        },

        initialize: function (options) {
            this.options = options;
            this.collection = new models.AccountTransactionCollection(options);
            this.collection.bind('change remove reset', this.renderWithData, this);
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        exportData: function (e) {
            e.preventDefault();
            if (this.collection.total == 0) {
                return;
            }
            if (this.collection.total > 1000) {
                notify.warning('警告', '数据量较大，请耐心等待');
            }
            utils.exportData("/admin/account/" + this.options.user_id + "/transaction/", this.options);
            return false;
        },
        doSearch: function () {
            var options = {},
                searched = {};
            searched.user_id = parseInt($("#searchUserId").val());
            searched.created_at_lower = $("#searchCreatedAtLower").val();
            searched.created_at_upper = $('#searchCreatedAtUpper').val();
            searched.price_lower = parseInt($("#searchPriceLower").val());
            searched.price_upper = parseInt($('#searchPriceUpper').val());
            searched.type = parseInt($('#searchType').val());
            searched.title = parseInt($('#searchTitle').val());

            if (!isNaN(searched.user_id)) {
                options.user_id = searched.user_id;
            }
            if (searched.created_at_lower || searched.created_at_upper) {
                options.created_at = {};
                if (searched.created_at_lower) options.created_at.$gte = searched.created_at_lower;
                if (searched.created_at_upper) options.created_at.$lt = searched.created_at_upper;
            }
            if(searched.type) {
                if (searched.type > 0) {
                    this.options.price = JSON.stringify({'$gte': 0});
                }
                else {
                    this.options.price = JSON.stringify({'$lte': 0});
                }
            }
            if (searched.price_lower || searched.price_upper) {
                options.price = {};
                if (searched.price_lower) options.price.$gte = searched.price_lower;
                if (searched.price_upper) options.price.$lt = searched.price_upper;
            }
            if(searched.title && searched.title.length > 0) {
                this.options.title = JSON.stringify({ '$in': searched.title });
            }
            utils.saveSearched('stats/transaction', searched);
            app.navigate(utils.composeQueryString('#stats/transaction/', options), {
                trigger: true
            });
        },
        renderWithData: function () {
            this.$el.html(this.template({
                items: this.collection.toJSON(),
                searched: utils.getSearched('stats/transaction'),
            }));
            var that = this;
            utils.renderTable('main-list', {
                $orderby: that.options.$orderby || '-created_at',
                sortCallback: function (field) {
                    that.options.$orderby = field;
                    that.options.$page = 1;
                    var newUrl = utils.composeQueryString('#stats/transaction/', that.options);
                    app.navigate(newUrl, {
                        trigger: true
                    });
                }
            });
            utils.multiselect('#searchTitle', true, {
                'includeSelectAllOption': true
            });
            $('[data-type=date]').datetimepicker({
                defaultDate: false,
                format: 'YYYY-MM-DD HH:mm:ss'
            });
            utils.getPaginator(that.options, this.collection.total, '#stats/transaction/');
            return this;
        },
        load: function () {
            this.collection.fetch({
                reset: true,
                data: this.options,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                    $('#content').append('<h4 class="text-muted">无数据</h4>');
                },
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "stats/TransactionList.html",
        initialize: function (options) {
            this.options = options;
        },
        render: function () {
            if (!this.options.user_id) this.options.user_id = 0;
            var view = new TransactionListView(this.options);
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();

            return this;
        }
    });

});
